// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tabs:[
        {
          id:0,
          name:"全部订单",
          isActive:true
        },
        {
        id:1,
        name:"使用中",
        isActive:false
      },{
        id:2,
        name:"待付款",
        isActive:false
      },{
        id:3,
        name:"已完成",
        isActive:false
      }],
      showOrders:[]
  },

  handleTap1(e){
    console.log(e.detail.index);
    wx.setStorageSync('orderType', e.detail.index)
    var that = this;
    var array = this.data.tabs;
    var user_info = wx.getStorageSync('user_info');
    var user_name = user_info.userName;
    for(var k =0;k<this.data.tabs.length;k++){
      if(k==e.detail.index){
        array[k].isActive=true;
        this.setData({
          tabs:array
        })
      }else{
        array[k].isActive=false;
        this.setData({
          tabs:array
        })
      }
    }
    wx.request({
      url: 'http://localhost:80/order/list?orderType='+e.detail.index+'&user_name='+user_name,
      method:'GET',
      success(res){
        console.log(res);
        that.setData({
          showOrders:res.data.data
        })
        console.log(that.data.showOrders)
      }
    })  
  },

  toOrderDetail(e){
    wx.setStorageSync('now_bind_order_info',this.data.showOrders[e.currentTarget.dataset.index])
    wx.navigateTo({
      url: '/pages/orderdetail/orderdetail',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    var user_info = wx.getStorageSync('user_info');
    var user_name = user_info.userName;
    var orderType = wx.getStorageSync('orderType');
    var array = this.data.tabs;
    if(orderType==null || orderType==0){
      for(var k =0;k<this.data.tabs.length;k++){
        if(k==orderType){
          array[k].isActive=true;
          this.setData({
            tabs:array
          })
        }else{
          array[k].isActive=false;
          this.setData({
            tabs:array
          })
        }
      }
        wx.request({
          url: 'http://localhost:80/order/list?orderType='+orderType+'&user_name='+user_name,
          method:'GET',
          success(res){
            console.log(res);
            that.setData({
              showOrders:res.data.data
            })
            console.log(that.data.showOrders)
          }
        })       
    }
    if(orderType==1){
      for(var k =0;k<this.data.tabs.length;k++){
        if(k==orderType){
          array[k].isActive=true;
          this.setData({
            tabs:array
          })
        }else{
          array[k].isActive=false;
          this.setData({
            tabs:array
          })
        }
      }
        wx.request({
          url: 'http://localhost:80/order/list?orderType='+orderType+'&user_name='+user_name,
          method:'GET',
          success(res){
            console.log(res);
            that.setData({
              showOrders:res.data.data
            })
            console.log(that.data.showOrders)
          }
        })       
    }
    if(orderType==2){
      for(var k =0;k<this.data.tabs.length;k++){
        if(k==orderType){
          array[k].isActive=true;
          this.setData({
            tabs:array
          })
        }else{
          array[k].isActive=false;
          this.setData({
            tabs:array
          })
        }
      }
        wx.request({
          url: 'http://localhost:80/order/list?orderType='+orderType+'&user_name='+user_name,
          method:'GET',
          success(res){
            console.log(res);
            that.setData({
              showOrders:res.data.data
            })
            console.log(that.data.showOrders)
          }
        })       
    }
    if(orderType==3){
      for(var k =0;k<this.data.tabs.length;k++){
        if(k==orderType){
          array[k].isActive=true;
          this.setData({
            tabs:array
          })
        }else{
          array[k].isActive=false;
          this.setData({
            tabs:array
          })
        }
      }
        wx.request({
          url: 'http://localhost:80/order/list?orderType='+orderType+'&user_name='+user_name,
          method:'GET',
          success(res){
            console.log(res);
            that.setData({
              showOrders:res.data.data
            })
            console.log(that.data.showOrders)
          }
        })       
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})